home *** CD-ROM | disk | FTP | other *** search
- // Copyright: © 1993 Apple Computer, Inc. All rights reserved.
- // Author: Scott Searle (original)
- // Victor J. Hnyp (extensions)
- // Dave Lyons (misc maintenance)
- // Date: 13-Jan-94
-
- // Revisions
- //
- // 05/01/94 JJ 3.03 use str255 parameter to open Users & Groups Data File
- // 01/16/94 GB 3.02 dont call GetMainDevice on non-CQD machines
- // 01/13/94 DAL 3.01 •Conditioned out DoGestaltCheck, since the one here never gets called.
- // UContext.cp short-circuits it and does the work itself.
- // •Changed LoWord() to (short) in DoHWBitDepthCheck (per Dec 93 code review).
- // •Changed DoMenuCheck to reflect that items past the first 32 are always
- // enabled (per Dec 93 code review).
- // •Fixed DoNumMonitorsCheck to use toolbox calls where available and only
- // count -active- screen devices (per Dec 93 code review).
- //
- // ------------------------------------------------------------------------------------------------------
- //
- // 03/24/93 VJH 2.06 Fixed: DoGestaltCheck (again!)
- //
- // 03/13/93 VJH 2.05 Fixed: isGuestAccess (again!)
- //
- // 03/11/93 VJH 2.04 Fixed: isGuestAccess, returned isVideoHWBitDepth to original design
- //
- // 02/08/93 VJH 2.03 Fixed: DoGestaltCheck, DoNameCheck, DoHWBitDepthCheck and DoNumMonitorsCheck
- //
- // 01/18/93 VJH 2.02 Fixed: Gestalt and menu checks
-
- #pragma load "AllHeaders.dump"
-
- #include "Utility.h"
- #include "Proto.h"
- #include "Context.h"
- #include "System.h"
-
- #define kComputerNameStrID (-16413)
-
- /* ------------------ Forward Declaration ---------------- */
- #if 0
- pascal Boolean DoGestaltCheck(GestaltStatePtr msg);
- #endif
- pascal Boolean DoMenuCheck(MenuIsAvailablePtr msg);
- pascal Boolean DoMonitorBitDepthCheck(BitDepthStatePtr msg);
- pascal Boolean DoNameCheck(ComputerNameInfoPtr msg);
- pascal Boolean DoHWBitDepthCheck(BitDepthStatePtr msg);
- pascal Boolean DoNumMonitorsCheck(BitDepthStatePtr msg);
- pascal Boolean DoFileSharingState(FileSharingPtr msg);
- pascal Boolean DoGuestAccessCheck(FileSharingPtr msg);
- MenuHandle GetIndexedMenuHandle(short index);
- void GetMenuTitle(MenuHandle menu, unsigned char* name);
- OSErr FindSysFolder(short *foundVRefNum, long *foundDirID);
-
-
- pascal OSErr main(ContextSelectorPtr msg, Size inSize,
- void* outMessage, Size* outSize, Handle /*startGlobals*/)
- {
- Boolean ret = false;
- OSErr err = errAECorruptData;
-
- if(inSize < sizeof(ContextSelector)) /* If inSize is < length of selector (a long), */
- return(err); /* return an error. Would be nice to have a specific error # */
-
- switch (msg->selector)
- {
- // Never gets called--see UContext.cp (13-Jan-94 DAL)
- #if 0
- case gestaltCheck:
- ret = DoGestaltCheck((GestaltStatePtr)msg);
- break;
- #endif
-
- case menuItemExists:
- case menuItemMarked:
- case menuItemEnabled:
- case menuItemDisabled:
- ret = DoMenuCheck((MenuIsAvailablePtr)msg);
- break;
-
- case isFileSharing :
- ret = DoFileSharingState((FileSharingPtr)msg);
- break;
-
- case isVideoHWBitDepth :
- ret = DoHWBitDepthCheck((BitDepthStatePtr)msg);
- break;
-
- case isMonitorBitDepth :
- ret = DoMonitorBitDepthCheck((BitDepthStatePtr)msg);
- break;
-
- case isComputerNamed :
- ret = DoNameCheck((ComputerNameInfoPtr)msg);
- break;
-
- case isGuestAccess :
- ret = DoGuestAccessCheck((FileSharingPtr)msg);
- break;
-
- case isNumberMonitors :
- ret = DoNumMonitorsCheck((BitDepthStatePtr)msg);
- break;
-
- default: /* None of the pre-defined types. Exit with error */
- break; /* Would be nice to have a specific error # */
- }
-
- err = SetContextResult(&ret, sizeof(Boolean), outMessage, outSize);
- return(err);
- }
-
-
- // Never gets called--see UContext.cp (13-Jan-94 DAL)
- #if 0
- pascal Boolean DoGestaltCheck(GestaltStatePtr msg)
- {
- long result;
- Boolean ret = false;
- OSErr err = errAECorruptData;
-
- if (err = Gestalt(msg->gestaltSelector, &result) == noErr)
- {
- switch (msg->compareSelector)
- {
- case equals:
- ret = result == msg->value;
- break;
- case notEqualTo:
- ret = result != msg->value;
- break;
- case greaterThan:
- ret = result > msg->value;
- break;
- case lessThan:
- ret = result < msg->value;
- break;
- case greaterThanOrEqualTo:
- ret = result >= msg->value;
- break;
- case lessThanOrEqualTo:
- ret = result <= msg->value;
- break;
- default:
- break;
- }
- }
-
- return(ret);
- }
- #endif
-
-
- pascal Boolean DoMonitorBitDepthCheck(BitDepthStatePtr msg)
- {
- Boolean ret = false;
- GDHandle theGD;
- short pixelDepth;
- SysEnvRec sysEnv;
-
- SysEnvirons( curSysEnvVers, &sysEnv );
- if(sysEnv.hasColorQD) {
- theGD = GetMainDevice(); // Can't do this on non-CQD machines
- pixelDepth = (**(**theGD).gdPMap).pixelSize;
- } else
- pixelDepth = 1;
-
- switch (msg->compareSelector)
- {
- case equals:
- ret = pixelDepth == msg->compareValue;
- break;
- case notEqualTo:
- ret = pixelDepth != msg->compareValue;
- break;
- case greaterThan:
- ret = pixelDepth > msg->compareValue;
- break;
- case lessThan:
- ret = pixelDepth < msg->compareValue;
- break;
- case greaterThanOrEqualTo:
- ret = pixelDepth >= msg->compareValue;
- break;
- case lessThanOrEqualTo:
- ret = pixelDepth <= msg->compareValue;
- break;
- default:
- break;
- }
- return(ret);
- } // DoMonitorBitDepthCheck
-
-
- pascal Boolean DoNameCheck(ComputerNameInfoPtr msg)
- {
- Str255 name;
- Boolean ret = false;
- OSErr err = noErr;
-
- if ((err = GetTheString(kComputerNameStrID, name)) == noErr)
- {
- ret = CompareStringSpec(name, &msg->computerName);
- }
-
- return(ret);
- } // DoNameCheck
-
-
- // 13-Jan-94 DAL -- This logic seems dubious to me. It's all based on the main monitor.
- // Maybe it would be better if equal/not-equal checked whether *any* monitor had the
- // target bit depth, and >/>= checked whether the deepest monitor has -at least- the target
- // bit depth, and </<= checked whether the shallowest monitor had less than the target bit
- // depth.
-
- pascal Boolean DoHWBitDepthCheck(BitDepthStatePtr msg)
- {
- Boolean ret = false;
- GDHandle theGD;
- short bitDepth;
- short mode;
- SysEnvRec sysEnv;
-
- SysEnvirons( curSysEnvVers, &sysEnv );
- if(!sysEnv.hasColorQD)
- {
- return(DoMonitorBitDepthCheck(msg)); // only has 1 bit depth on this non-CQD machine, so we don't need to do the HasDepth calls below
- }
-
- theGD = GetMainDevice(); // dont do this in non-CQD machines
- bitDepth = (short)( msg->compareValue );
- mode = HasDepth(theGD, bitDepth, 1, 1);
-
- switch (msg->compareSelector)
- {
- case equals:
- if( mode )
- ret = true;
- break;
- case notEqualTo:
- if( ! mode )
- ret = true;
- break;
- case greaterThan:
- bitDepth = bitDepth << 1;
- mode = HasDepth(theGD, bitDepth, 1, 1);
- if( mode )
- ret = true;
- break;
- case lessThan:
- bitDepth = bitDepth >> 1;
- mode = HasDepth(theGD, bitDepth, 1, 1);
- if( mode )
- ret = true;
- break;
- case greaterThanOrEqualTo:
- if( mode )
- ret = true;
- else
- {
- bitDepth = bitDepth << 1;
- mode = HasDepth(theGD, bitDepth, 1, 1);
- if( mode )
- ret = true;
- }
- break;
- case lessThanOrEqualTo:
- if( mode )
- ret = true;
- else
- {
- bitDepth = bitDepth >> 1;
- mode = HasDepth(theGD, bitDepth, 1, 1);
- if( mode )
- ret = true;
- }
- break;
- default:
- break;
- }
-
- return(ret);
- } // DoHWBitDepthCheck
-
-
- pascal Boolean DoNumMonitorsCheck(BitDepthStatePtr msg)
- {
- Boolean ret = false;
- GDHandle theGD;
- short numMonitors = 0;
- SysEnvRec sysEnv;
-
- // fix the nonCQD crash
- SysEnvirons( curSysEnvVers, &sysEnv );
- if(sysEnv.hasColorQD) {
- theGD = GetDeviceList(); // Can't do this on non-CQD machines
-
- while( theGD ) {
- if( TestDeviceAttribute(theGD, screenDevice) && TestDeviceAttribute(theGD, screenActive) )
- numMonitors++;
-
- theGD = GetNextDevice(theGD);
- }
- } else
- numMonitors = 1;
-
- #if 0
- theGD = GetDeviceList();
-
- while( theGD ) {
- if( TestDeviceAttribute(theGD, screenDevice) && TestDeviceAttribute(theGD, screenActive) )
- numMonitors++;
-
- theGD = GetNextDevice(theGD);
- }
- #endif
-
- switch (msg->compareSelector)
- {
- case equals:
- ret = numMonitors == msg->compareValue;
- break;
- case notEqualTo:
- ret = numMonitors != msg->compareValue;
- break;
- case greaterThan:
- ret = numMonitors > msg->compareValue;
- break;
- case lessThan:
- ret = numMonitors < msg->compareValue;
- break;
- case greaterThanOrEqualTo:
- ret = numMonitors >= msg->compareValue;
- break;
- case lessThanOrEqualTo:
- ret = numMonitors <= msg->compareValue;
- break;
- default:
- break;
- }
- return(ret);
- } // DoNumMonitorsCheck
-
-
- pascal Boolean DoMenuCheck(MenuIsAvailablePtr msg)
- {
- MenuHandle menu;
- Str255 menuTitle;
- Str255 menuItem;
- short i, j;
- short numItems;
- short mark;
- unsigned long enableFlags;
- unsigned long mask;
- unsigned long result;
- short theItem = 0;
- MenuHandle theMenu = nil;
- Boolean ret = false;
- Boolean foundIt = false;
-
- StringSpecPtr secondSP; // Need to get around record structure limitation
-
- secondSP = (StringSpecPtr)GetNextItemAddress( &msg->menuTitle.str );
-
- // addresses
- // DebugStr( (char*)N2S( &msg->mSelector ));
- // values
- // DebugStr( (char*)N2S( msg->mSelector ));
-
- for (i = 0; !foundIt && (menu = GetIndexedMenuHandle(i)); ++i)
- {
- GetMenuTitle(menu, menuTitle);
-
- if (CompareStringSpec(menuTitle, &msg->menuTitle))
- for (j = 1, numItems = CountMItems(menu); j <= numItems; ++j)
- {
- GetItem(menu, j, menuItem);
-
- if (CompareStringSpec(menuItem, secondSP))
- {
- theMenu = menu;
- theItem = j;
- foundIt = true;
- break;
- }
- }
- }
-
- if (foundIt && theMenu && theItem)
- {
- switch (msg->mSelector)
- {
- case menuItemExists:
- ret = foundIt;
- break;
-
- case menuItemMarked:
- GetItemMark(theMenu, theItem, &mark);
- ret = (mark != 0);
- break;
-
- case menuItemEnabled:
- case menuItemDisabled:
- if(theItem > 32)
- {
- result = 1; // Items beyond the 1st 32 are always enabled
- }
- else
- {
- enableFlags = (unsigned long) (**theMenu).enableFlags;
- mask = (unsigned long) ((unsigned long) 1 << (theItem - 1));
- result = (unsigned long) (enableFlags & mask);
- }
- ret = (msg->mSelector == menuItemEnabled) ?
- result != (unsigned long) 0 :
- result == (unsigned long) 0;
- break;
- }
- }
-
- return(ret);
- } // DoMenuCheck
-
-
-
- pascal Boolean VolIsSharable(short vRefNum)
- // See if local File Sharing is enabled on the volume specified by vRefNum
- {
- HIOParam pb;
- GetVolParmsInfoBuffer infoBuffer;
- OSErr err;
- Boolean ret = false;
- char nameString[32];
-
- pb.ioNamePtr = nil;
- pb.ioVRefNum = vRefNum;
- pb.ioBuffer = (Ptr)&infoBuffer;
- pb.ioReqCount = sizeof(infoBuffer);
- pb.ioNamePtr = nameString;
-
- err = PBHGetVolParmsSync((HParmBlkPtr)&pb);
- if(err == noErr)
- if(infoBuffer.vMAttrib & 0x0100 ) // bit 8 = bHasPersonalAccessPrivileges flag
- ret = true;
-
- return( ret );
- } // VolIsSharable;
-
-
-
- pascal Boolean DoFileSharingState(FileSharingPtr msg)
- //See if File Sharing is turned on by seeing if any volume has
- //local File Sharing enabled
- {
- HVolumeParam pb;
- OSErr err = noErr;
- short volIndex = 1;
- Boolean sharing = false;
- Boolean ret = false;
- char nameString[32];
-
- do
- {
- pb.ioNamePtr = nil;
- pb.ioVolIndex = volIndex;
- pb.ioNamePtr = nameString;
-
- err = PBHGetVInfoSync((HParmBlkPtr)&pb);
- if( err == noErr )
- sharing = VolIsSharable(pb.ioVRefNum);
-
- volIndex += 1;
- }
- while ((err == noErr) &&
- ( !sharing )); //stop if error or if a volume has local File Sharing enabled
-
- switch (msg->compareValue)
- {
- case on:
- ret = sharing == true;
- break;
- case off:
- ret = sharing == false;
- break;
- default:
- break;
- }
-
- return( ret );
- }
-
-
- /*
- pascal Boolean DoGuestAccessCheck(FileSharingPtr msg)
- //See what priveleges guests have for startup volume when hooking in via Guest Access
- {
- OSErr err = noErr;
- Boolean ret = false;
- short startupVolRefNum;
- long systemDirID;
- HParamBlockRec thePB;
- AccessParam *hapb = (AccessParam*)&thePB;
- HFileParam *hfpb = (HFileParam*)&thePB;
- char nameString[32];
- OSErr localError;
- short thePrivs;
-
- if( FindSysFolder( &startupVolRefNum, &systemDirID )) // Error getting volume ref num of startup volume
- return( ret );
-
- // Set the param block up for getting information about the startup volume sharing
- thePB.accessParam.ioCompletion = nil;
- thePB.accessParam.ioVRefNum = startupVolRefNum; // Use the startup volume reference for lookup
- thePB.fileParam.ioDirID = fsRtDirID; // Use the root directory for lookup
- thePB.accessParam.ioNamePtr = nameString;
-
- // Get information on the startup volume SYNCHRONOUSLY, by passing pointer to our param block
- if (localError = PBHGetDirAccess((HParmBlkPtr)&thePB,false)) // Error getting information
- return( ret );
-
- thePrivs = HiWord( thePB.accessParam.ioACAccess );
-
- // Note - We can ignore bit 28 (blank access privileges) for root directories
-
- switch (msg->compareValue)
- {
- case seeFolders:
- ret = thePrivs & 1;
- break;
- case seeFiles:
- ret = thePrivs & 2;
- break;
- case makeChanges:
- ret = thePrivs & 4;
- break;
- default:
- break;
- }
-
- return( ret );
- } // DoGuestAccessCheck
- */
-
-
- pascal Boolean DoGuestAccessCheck(FileSharingPtr msg)
- //See if Guest Access is turned on by checking byte within "Users & Groups Data File"
- {
- OSErr err = noErr;
- Boolean ret = false;
- short startupVolRefNum = 0;
- long theDirID = 0;
- long bytesToRead;
- unsigned char theVal;
- short fRefNum;
-
- if( FindFolder( kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &startupVolRefNum, &theDirID ))
- return( ret );
-
- if( HOpenDF( startupVolRefNum, theDirID, msg->filename, fsRdPerm, &fRefNum ))
- return( ret );
-
- if( SetFPos( fRefNum, fsFromStart, 0x1B )) // "Record Number" of machine name
- return( ret );
-
- bytesToRead = 1;
- if( FSRead( fRefNum, &bytesToRead, &theVal ))
- return( ret );
-
- if( SetFPos( fRefNum, fsFromStart, theVal * 0x200 + 0x3C )) // Byte of guest access privileges
- return( ret );
-
- bytesToRead = 1;
- if( FSRead( fRefNum, &bytesToRead, &theVal ))
- return( ret );
-
- if( FSClose( fRefNum ))
- return( ret );
-
- switch (msg->compareValue)
- {
- case on:
- if( !( theVal & 2 ))
- ret = true;
- break;
- case off:
- if( theVal & 2 )
- ret = true;
- break;
- default:
- break;
- }
-
- return( ret );
- } // DoGuestAccessCheck
-
-
-
- MenuHandle GetIndexedMenuHandle(short index)
- {
- short numMenus;
- MenuRecPtr menuRecList;
- MenuListRecHdl mList = (MenuListRecHdl)MenuList;
- MenuHandle menu = nil;
-
- if (mList)
- {
- numMenus = (**mList).lastMenu / sizeof(MenuRec);
- menuRecList = (MenuRecPtr)&(**mList).startMenuRecList;
-
- if (index >= 0 && index < numMenus)
- menu = menuRecList[index].menuHdl;
- }
-
- return(menu);
- } // GetIndexedMenuHandle
-
-
-
- void GetMenuTitle(MenuHandle menu, unsigned char* name)
- {
- BlockMove(&(**menu).menuData, name, (long)((**menu).menuData[0] + 1));
- } // GetMenuTitle
-
-
-
- OSErr FindSysFolder(short *foundVRefNum, long *foundDirID)
- {
- OSErr err;
-
- *foundVRefNum = 0;
- *foundDirID = 0;
- err = FindFolder( kOnSystemDisk, kSystemFolderType, kDontCreateFolder, foundVRefNum, foundDirID );
- return( err );
- } // FindSysFolder();
-
-
-
- OSErr FindFile(char *filename, short theVolume, long theDirID)
- {
- OSErr err;
- HFileInfo thePB;
-
- /* Set the param block up for getting information about the given file */
- thePB.ioCompletion = nil;
- thePB.ioNamePtr = filename; // File name we want
- thePB.ioVRefNum = theVolume; // Volume to be searched
- thePB.ioFDirIndex = 0; // Use the filename to find our file
- thePB.ioDirID = theDirID; // The directory to be searched
-
- if (err = PBGetCatInfo((CInfoPBPtr)&thePB,false)) // Error getting information
- return( err );
-
- return( 0 );
- } // FindFile();
-
-
-